class Iros {
    static afterMethod

    constructor(config) {
        this.config = config;
        this.result = '';
    }

    //请求拦截器
    before(method) {
        method(this.config);
    }

    //get请求和post请求
    get(apiName, data) {
        console.log(this.config)
        let {baseUrl, api} = this.config;
        return Iros.request(`${baseUrl}${api[apiName]}`, data, 'GET')
    }

    post(apiName, data) {
        let {baseUrl, api} = this.config;
        return Iros.request(`${baseUrl}/${api[apiName]}`, data, 'POST')
    }

    static request(url, data, method) {
        let _this = this;
        return new Promise((resolve, reject) => {
            uni.request({
                ..._this.config,
                url,
                data,
                method,
            }).then(res => {
                _this.result = res;
                if (typeof Iros.afterMethod == "function") _this.result = Iros.afterMethod(res)
                resolve(_this.result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    //响应拦截器
    after(method) {
        Iros.afterMethod = method
    }
}

export default Iros;
